package com.niit.mycourse.provider.impl;

import com.niit.entity.Course;
import com.niit.mycourse.provider.CourseMessageProvider;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;

@EnableBinding(Source.class)
public class CourseMessageProviderImpl implements CourseMessageProvider {
  //系统提供类用于发送消息
  @Resource
  @Output("course")
  private MessageChannel output;

  @Override
  public void send(Course course) {
    Message<Course> build = MessageBuilder.withPayload(course).build();
    boolean sendFlag = output.send(build);
    if (sendFlag) {
      System.out.print("消息发送成功: "+course);
    }else {
      System.out.print("消息发送失败: " + course);
    }
  }
}